package com.jyb.jybmanage;


import org.json.JSONException;
import org.json.JSONObject;

import com.example.jybmanage.R;
import com.jyb.util.SendRequest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
	
	private EditText accountView,passwordView;
	private Button longinButton;
	private String account =null;
	private String password=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        initView();
        addListener();
		
	}

	private void addListener() {
		longinButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				account = accountView.getText().toString();
				password = passwordView.getText().toString();
				longinButton.setEnabled(false);
				new Thread(runnable).start();
			}

			
		});
		
	}
	
	//启动新线程进行登录
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			String string = login(account,password);
			try {
				JSONObject result = new JSONObject(string);
				mHandler.obtainMessage(("null".equals(result.getString("code")) || null==result.get("code")) ? 0 : result.getInt("code"), result.get("message")).sendToTarget();;
			} catch (JSONException e) {
				mHandler.obtainMessage(0, "未知错误！").sendToTarget();;
				e.printStackTrace();
			}
			
		}
	};
	
	//登录操作线程与UI线程通信
	private Handler mHandler = new Handler() {  
        public void handleMessage (Message msg) { 
        	switch(msg.what) {  
            case 0:
                Toast.makeText(getApplication(),(String)msg.obj, Toast.LENGTH_LONG).show();
                longinButton.setEnabled(true);
                break;  
            case 1: 
            	JYBApplication application = (JYBApplication) getApplication();
            	application.setAccount(account);
            	application.setPassword(password);
            	application.setLogin_status(true);
            	Intent intent = new Intent(MainActivity.this,IndexActicity.class);  
            	MainActivity.this.startActivity(intent);  
                //Toast.makeText(getApplication(), (String)msg.obj, Toast.LENGTH_LONG).show();
                break;  
            }  
        }  
    };  
	public String login(String account, String password) {
		SendRequest sendRequest = new SendRequest();
		return sendRequest.login(account, password);
	}
	private void initView() {
		accountView = (EditText) findViewById(R.id.userName);
		passwordView = (EditText) findViewById(R.id.passWord);
		longinButton = (Button) findViewById(R.id.login);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
